import Vue from 'vue'
import VueRouter from 'vue-router'
import jwtDecode from 'jwt-decode'
Vue.use(VueRouter)

const routes = [{
    path: '/login',
    name: 'login',
    component: () => import( /* webpackChunkName: "about" */ '../views/Login.vue')
  },
  {
    path: '/index',
    name: 'index',
    redirect: '/welcome', // 重定向:重新指向其它path,会改变网址
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import( /* webpackChunkName: "about" */ '../views/Index.vue'),
    children: [{
        path: '/welcome',
        name: 'welcome',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import( /* webpackChunkName: "about" */ '../views/welcome.vue')
      },
      {
        path: '/users',
        name: 'users',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import( /* webpackChunkName: "about" */ '../views/Users.vue')
      },
      {
        path: '/roles',
        name: 'roles',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import( /* webpackChunkName: "about" */ '../views/roles.vue')
      },
      {
        path: '/rights',
        name: 'rights',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import( /* webpackChunkName: "about" */ '../views/rights.vue')
      },
      {
        path: '/goods',
        name: 'goods',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import( /* webpackChunkName: "about" */ '../views/goods.vue')
      },
      {
        path: '/params',
        name: 'params',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import( /* webpackChunkName: "about" */ '../views/Paramss.vue')
      },
      {
        path: '/categories',
        name: 'categories',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import( /* webpackChunkName: "about" */ '../views/categories.vue')
      },
      {
        path: '/orders',
        name: 'orders',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import( /* webpackChunkName: "about" */ '../views/orders.vue')
      },
      {
        path: '/reports',
        name: 'reports',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import( /* webpackChunkName: "about" */ '../views/reports.vue')
      },
      {
        path: '/add',
        name: 'add',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import( /* webpackChunkName: "about" */ '../views/Add.vue')
      },{
        path: '/about',
        name: 'about',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import( /* webpackChunkName: "about" */ '../views/AboutView.vue')
      },
    ]
  }
]

const router = new VueRouter({
  routes
})
// router.beforeEach((to, from, next) => {
//   let token = localStorage.getItem('token')

  // if (token) {
  //   let result = jwt_decode(token)
  //   console.log(result, "99999999");
  //   let nowTime = parseInt(new Date().getItem() / 1000)
  //   if (nowTime - result.exp > 0) {
  //     next('/login')
  //   } else {
  //     next()
  //   }
  // } else {
  //   next('/login')
  // }
  // if (to.path != '/login' && !localStorage.getItem('token')) {
  //   next('/login')
  // } else {
  //   next()
  // }

// })
router.beforeEach((to, from, next) => {
  //登录页面，直接放行
  if (to.path == '/login') {
    return next();
  }
  //查看token
  let token = localStorage.getItem('token');
  // 判断token是否失效
  if (token) {
    //解析token
    let result = jwtDecode(token);
    let nowTime = parseInt(new Date().getTime() / 1000);
    // console.log(result);
    if (nowTime - result.exp > 0) {
      next('/login');
    } else {
      next();
    }
  } else {
    next('/login');
  }
});
export default router